﻿@namespace Masa.Blazor
@inherits MItemGroup

<CascadingValue Value="this" IsFixed>
    <div class="@GetClass()" style="@GetStyle()" id="@Id" @ref="Ref" @attributes="@Attributes">
        @GenAffix("prev", HasPrev, PrevContent, PrevIcon)
        <div class="@_block.Element("wrapper")" @ref="WrapperRef">
            <div class="@GetContentClass()" @ref="ContentRef">
                @ChildContent
            </div>
        </div>
        @GenAffix("next", HasNext, NextContent, NextIcon)
    </div>
</CascadingValue>

@code {

    private RenderFragment GenAffix(string affix, bool hasAffix, RenderFragment? content, string? icon) => __builder =>
    {
        if (HasAffixes)
        {
            var disabled = !hasAffix;
            var css = GetAffixModifierBuilder(affix).Add(disabled);

            <div class="@css"
                 @onclick="@(() => HandleOnAffixClick(affix))">
                @if (content != null)
                {
                    @content
                }
                else
                {
                    @*TODO: fade transition*@
                    <MIcon Disabled="@disabled" Icon="@icon"/>
                }
            </div>
        }
    };

    private ModifierBuilder _prevModifierBuilder = _block.Element("prev").CreateModifierBuilder();
    private ModifierBuilder _nextModifierBuilder = _block.Element("next").CreateModifierBuilder();

    private ModifierBuilder GetAffixModifierBuilder(string affix) => affix switch
    {
        "prev" => _prevModifierBuilder,
        "next" => _nextModifierBuilder,
        _ => throw new ArgumentException($"Invalid affix: {affix}")
    };

}